package com.project.service.impl;

import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public void add(ManBean man) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.add(man);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, LocalDate birthday) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.update(id,birthday);
        session.commit();
        session.close();
    }

    @Override
    public ManBean findById(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        ManBean man = mapper.findById(id);
        session.close();
        return man;
    }

    @Override
    public List<ManBean> findAll() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public List<ManBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        Map map = new HashMap();
        map.put("name",name);
        map.put("startDate",startDate);
        map.put("endDate",endDate);

        List<ManBean> list = mapper.findByItem(map);
        session.close();
        return list;
    }
}
